package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonHighlight;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Threads;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class ForumItem extends BaseForumElement
	{
		private const DIVIDER_COUNT:int = 3;
		
		public var title:TextField;
		public var description:TextField;
		public var posts:TextField;
		public var threads:TextField;
		public var lastPost:TextField;
				
		//setup the vars to hold on to stuff for mouse clicks and junk
		private var forumID:int;
		private var forumLastThreadID:int;
		//private var forumLastThreadName:String;
		private var forumReadMode:int;
		
		//vars to share
		private var categoryHeader:CategoryHeader;
		private var mainButton:ButtonHighlight; 
		private var postButton:ButtonHighlight;
		
		public function ForumItem(title:String, description:String, posts:String, threads:String, lastPost:String, lastUser:String, categoryHeader:CategoryHeader){
			//set the CSS
			this.title.styleSheet = Styles.css;
			this.description.styleSheet = Styles.css;
			this.posts.styleSheet = Styles.css;
			this.threads.styleSheet = Styles.css;
			this.lastPost.styleSheet = Styles.css;
						
			//setup the textfields
			this.title.htmlText = "<span class=\"forumTitle\">" + title + "</span>";
			this.description.htmlText = "<span class=\"forumDescription\">" + description + "</span>";
			this.posts.htmlText = "<span class=\"forumPosts\">" + posts + "</span>";
			this.threads.htmlText = "<span class=\"forumThreads\">" + threads + "</span>";
			this.lastPost.htmlText = "<span class=\"forumLastPost\">" + lastPost + "<br>" + Language.main(Word.BY, false, true) + " " + lastUser + "</span>";
			
			//if there are no posts
			if(lastPost == "0"){
				this.lastPost.autoSize = TextFieldAutoSize.LEFT;
				this.lastPost.htmlText = "<span class=\"forumLastPost\">" + Language.main(Word.NO_POSTS) + "</span>";
				this.lastPost.y = Math.round(background.height/2 - this.lastPost.height/2);
			}
			
			//share the header
			this.categoryHeader = categoryHeader;
			
			//add a couple of buttons to click
			mainButton = new ButtonHighlight(this, viewThreads);
			addChild(mainButton);
			
			postButton = new ButtonHighlight(this, viewPost);
			addChild(postButton);
			
			//handle the defaults
			super.init(".forumBackground", DIVIDER_COUNT);
		}
		
		public function setProperties(id:int, lastThreadID:int, readMode:int, lastRead:int):void {
			forumID = id;
			forumLastThreadID = lastThreadID;
			forumReadMode = readMode;
						
			//attach a forum status icon and put it where it needs to go
			var statusIcon:StatusIcon = new StatusIcon(StatusIcon.OLD);
			
			//check if we have some new posts. -1 means we don't
			if(lastRead >= 0){
				statusIcon.setStatus(StatusIcon.NEW);
			}
			statusIcon.x = Config.TEXT_PADDING;
			statusIcon.y = Math.round((height/2) - (statusIcon.height/2));
			addChildAt(statusIcon, getChildIndex(mainButton)-1);
			
			//move the title and description
			title.x = statusIcon.x + statusIcon.width + Config.TEXT_PADDING;
			description.x = title.x;
			
			//if the forum is read only let's put a lock on it
			if(readMode == 1){
				var lock:StatusIcon = new StatusIcon(StatusIcon.LOCKED);
				lock.x = statusIcon.x;
				lock.y = statusIcon.y;
				addChildAt(lock, getChildIndex(statusIcon)+1);
			}
		}
		
		override public function resize():void {
			placeStuff();
			
			super.resize();
		}
		
		public function set enableHighlights(isEnabled:Boolean):void {
			mainButton.enabled = isEnabled;
			mainButton.useHandCursor = isEnabled;
			postButton.enabled = isEnabled;
			postButton.useHandCursor = isEnabled;
		}
		
		private function placeStuff():void {
			//places stuff where it needs to go
			title.width = categoryHeader.getDivider(0).x - title.x;
			description.width = categoryHeader.getDivider(0).x - description.x;
			posts.x = categoryHeader.posts.x;
			threads.x = categoryHeader.threads.x;
			lastPost.x = categoryHeader.lastPost.x;
			
			//handle dividers
			for(var i:int = 0; i < DIVIDER_COUNT; i++){
				getDivider(i).x = categoryHeader.getDivider(i).x;
			}
			
			//do the button sizes
			mainButton.width = this.width;
			postButton.x = categoryHeader.getDivider(2).x + categoryHeader.getDivider(2).width;
			postButton.width = MXP3.wStage - postButton.x;
		}
		
		private function viewThreads(event:MouseEvent):void {
			Threads.isForumRestricted = forumReadMode;
			DeepLinking.setLink(Word.THREADS, DeepLinking.createLinkArray(forumID));
		}
		
		private function viewPost(event:MouseEvent):void {
			Threads.isForumRestricted = forumReadMode;
			
			//make sure we have a thread to go to
			if(forumLastThreadID > 0){
				DeepLinking.setLink(Word.POSTS, DeepLinking.createLinkArray(forumLastThreadID));
			}
		}
		
	}
}